/**
 * this class to find destination file in the disc
 */
package cn.netjava.softkid.chapter06;

import java.io.File;

/**
 * @author softkid
 * 
 */
public class DiscFile {
	/**
	 * list the disc of this computer
	 */
	public void listRoot() {
		File[] file = File.listRoots();
		for (int i = 0; i < file.length; i++) {
			System.out.println(file[i]);
		}
	}

	/**
	 * search file in the destination disc
	 * 
	 * @param disc:which disc you want to search in
	 * @param file:the file you want to search for
	 */
	public void findFile(String disc, String name) {
		File list = new File(disc);
		File[] fileList = list.listFiles();
		if (fileList != null) {
			// System.out.println(fileList.length);
			for (int i = 0; i < fileList.length; i++) {
				File temp = fileList[i];
				String fileName = temp.getName();
				// output all the file name under disc
				// System.out.println(fileName);

				if (temp.isFile() && fileName.equals(name)) {
					find = true;
					break;
				} else if (temp.isDirectory()) {
					findFile(temp.getParent() + "/" + fileName, name);
				}
				// System.out.println(temp);
			}

		}

	}
	/**
	 * see whether destination was find
	 * @return: find
	 */
	public boolean isFind() {
		return find;
	}

	public static void main(String args[]) {
		DiscFile df = new DiscFile();
		// df.listRoot();
		df.findFile("D:", "test.txt");
		if(df.isFind())
		{
			System.out.println("Destination file was find!");

		}
		else
		{
			System.out.println("Destination file was not find!");

		}
	}
	boolean find = false;

}
